package com.example.demo.controller;

import com.example.demo.po.dzmodel;
import com.example.demo.po.plmodel;
import com.example.demo.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@CrossOrigin(origins = "*")
//@RestController
@Controller
public class AllController {
    @Autowired
    private Service Service;

    //    进入index00
    @RequestMapping("/index00")
    public String index00() {
        return "index00";
    }

    //    进入index01
    @RequestMapping("/index01")
    public String index01() {
        return "index01";
    }

    //    进入index02
    @RequestMapping("/index02")
    public String index02() {
        return "index02";
    }

    //    进入index03
    @RequestMapping("/index03")
    public String index03() {
        return "index03";
    }

    //    进入ht
    @RequestMapping("/ht")
    public String ht() {
        return "ht";
    }


    //    查询所有评论
    //    添加评论
    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<plmodel> list = this.Service.findAll();
        model.addAttribute("list", list);
        return "index00";
    }

    //    查出点赞数和所有评论ht显示
    //    @ResponseBody
    @RequestMapping("/selectht")
    public String selectht(Model model) {
        int dz = this.Service.selectdz();
        model.addAttribute("dzInfo", dz);
        List<plmodel> list=Service.findAll();
        model.addAttribute("list", list);
        System.out.println(dz);
        return "ht";
    }

    //    添加评论
    @RequestMapping("/add")
    public String add(Model model, String pltext) {
//        String pltext = request.getParameter("pltext");
        if (pltext.isEmpty()){
            return "redirect:/findAll";
        }else {
            Service.add(pltext);
            System.out.println(pltext);
            return "redirect:/findAll";
        }

    }

    //    删除评论
    @RequestMapping("/delete")
    public String delete(int id) {
        Service.delete(id);
        return "redirect:selectht";
    }

//    //    查询所有评论在后台
//    @RequestMapping("/findAll01")
//    public String findAll01(Model model) {
//        List<plmodel> list = this.Service.findAll();
//        model.addAttribute("list", list);
//        return "ht";
//    }

//    登录
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        if ("lhq".equals(name)&&"123456".equals(password)){
            return "redirect:selectht";
        }else {
            return "redirect:index02";
        }

    }

//    点赞实现
    @RequestMapping("/countdz")
    public String countdz(HttpServletRequest request){
        int number=Integer.parseInt(request.getParameter("count_btn_1"));
        int dz=Service.selectdz();
        System.out.println("当前"+dz);
        int countnum=dz+number;
        System.out.println("之后"+countnum);
        Service.countdz(countnum);
        return "redirect:/findAll";
    }
}